<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<meta charset="utf-8">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link rel="shortcut icon" href="favicon.ico">
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet"/>
    <link th:href="@{/css/layui.css}" rel="stylesheet" >
    <link th:href="@{/css/font-awesome.css}" rel="stylesheet"/>
    <!-- bootstrap-table 表格插件样式 -->
    <link th:href="@{/ajax/libs/bootstrap-table/bootstrap-table.min.css}" rel="stylesheet"/>
    <link th:href="@{/ajax/libs/jqTreeGrid/jquery.treegrid.css}" rel="stylesheet"/>
    <link th:href="@{/css/animate.css}" rel="stylesheet"/>
    <link th:href="@{/css/style.css}" rel="stylesheet"/>
    <link th:href="@{/css/checkbox.css}" rel="stylesheet"/>
    <link th:href="@{/ajax/libs/select/select2.css}" rel="stylesheet"/>
    <link th:href="@{/ruoyi/css/ry-ui.min.css}" rel="stylesheet"/>
    <!-- Data Tables -->
    <link th:href="@{/css/plugins/dataTables/dataTables.bootstrap.css}" rel="stylesheet">
    <link href="/ajax/libs/jquery-layout/jquery.layout-latest.css" th:href="@{/ajax/libs/jquery-layout/jquery.layout-latest.css}" rel="stylesheet"/>
    <link href="/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css" th:href="@{/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css}" rel="stylesheet"/>
</head>
<link href="/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css" th:href="@{/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css}" rel="stylesheet"/>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-role-edit" >
        <input id="roleId" name="roleId" type="hidden" />
        <div class="form-group">
            <label class="col-sm-3 control-label ">角色名称：</label>
            <div class="col-sm-8">
                <input class="form-control" th:value="${role.roleDescription}" readonly="true" type="text" name="roleName" id="roleName" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">角色字符：</label>
            <div class="col-sm-8">
                <input class="form-control" th:value="${role.role}" readonly="true" type="text" name="roleKey" id="roleKey" >
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">菜单权限</label>
            <div class="col-sm-8">
                <div id="menuTrees" class="ztree"></div>
            </div>
        </div>
        <div class="form-group">
            <div class="form-control-static col-sm-offset-9">
                <button type="button" onclick="edit()" class="btn btn-primary">提交</button>
                <button onclick="$.modal.close()" class="btn btn-danger" type="button">关闭</button>
            </div>
        </div>
    </form>
</div>
<script th:src="@{/js/jquery.min.js}"></script>
<script th:src="@{/js/bootstrap.min.js}"></script>
<!-- bootstrap-table 表格插件 -->
<script th:src="@{/ajax/libs/bootstrap-table/bootstrap-table.min.js}"></script>
<script th:src="@{/ajax/libs/bootstrap-table/locale/bootstrap-table-zh-CN.min.js}"></script>
<script th:src="@{/ajax/libs/bootstrap-table/extensions/mobile/bootstrap-table-mobile.min.js}"></script>
<script th:src="@{/ajax/libs/bootstrap-table/extensions/toolbar/bootstrap-table-toolbar.min.js}"></script>
<!-- jquery-export 表格导出插件 -->
<script th:src="@{/ajax/libs/bootstrap-table/extensions/export/bootstrap-table-export.js}"></script>
<script th:src="@{/ajax/libs/bootstrap-table/extensions/export/tableExport.js}"></script>
<!-- Data Tables -->
<script th:src="@{/js/plugins/dataTables/jquery.dataTables.js}"></script>
<script th:src="@{/js/plugins/dataTables/dataTables.bootstrap.js}"></script>
<script th:src="@{/js/plugins/jeditable/jquery.jeditable.js}"></script>
<script th:src="@{/ajax/libs/jquery-layout/jquery.layout-latest.js}"></script>
<script th:src="@{/ajax/libs/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js}"></script>
<script th:src="@{/ajax/libs/layer/layer.min.js}"></script>
<script th:src="@{/ajax/libs/layui/layui.js}"></script>
<script th:src="@{/ruoyi/js/common.js?v=2.2.0}"></script>
<script th:src="@{/ruoyi/js/yuyu-ui.min.js?v=2.2.0}"></script>
<script th:inline="javascript">
    var role = [[${role}]];
    // 树结构初始化加载
    var setting = {
        check:{enable:true,nocheckInherit:true,chkboxType:{"Y":"ps","N":"ps"}},
        view:{selectedMulti:false,nameIsHTML: true},
        data:{simpleData:{enable:true}},
        callback:{
            beforeClick: function (treeId, treeNode, clickFlag) {
                var menuTrees = $.fn.zTree.getZTreeObj(treeId);
                menuTrees.checkNode(treeNode, !treeNode.checked, true, true);
                return false;
            }
        }
    }, menuTrees, loadTree = function(){
        $.get("/permission/list/"+role.roleId, function(data) {
            menuTrees = $.fn.zTree.init($("#menuTrees"), setting, data).expandAll(true);
        }, null, null, "正在加载，请稍后...");
    };  loadTree();


    function selectCheckeds() {
        var zTreeOjb = $.fn.zTree.getZTreeObj("menuTrees");  //ztree的Id  zTreeId
     var treeNodes = zTreeOjb.getCheckedNodes();
        // var menuIds = "";
        // for (var i = 0; i < treeNodes.length; i++) {
        //     if (0 == i) {
        //         menuIds = treeNodes[i].id;
        //     } else {
        //         menuIds += ("," + treeNodes[i].id);
        //     }
        // }
        return treeNodes;
    }

    function edit() {
        // var roleId = $("input[name='roleId']").val();
        // var roleName = $("input[name='roleName']").val();
        // var roleKey = $("input[name='roleKey']").val();
        // var roleSort = $("input[name='roleSort']").val();
        // var status = $("input[name='status']").is(':checked') == true ? 0 : 1;
        // var remark = $("input[name='remark']").val();
        var menuIds = selectCheckeds();
        var param=[];
        for (var a =0;a<menuIds.length;a++){
            param.push({"id":menuIds[a].id});
        }
        $.ajax({
            type : "POST",
            url : "/role/authority/"+role.roleId,
            dataType: 'json',
            data : JSON.stringify(param),
            contentType:"application/json",
            error : function(request) {
                $.modal.alertError("系统错误");
            },
            success : function(data) {
               if (data.code==200){
                   parent.layer.msg('授权成功', {icon: 1});
                   setTimeout(function () {
                       var index = parent.layer.getFrameIndex(window.name);
                       parent.layer.close(index);
                   },500);
               }
            }
        });
    }
</script>
</body>
</html>